package com.cjy.userReport.service.complaint.impl;

import com.base.common.ConfigConstants;
import com.base.common.entity.ComplaintTicket;
import com.base.common.enums.TicketCategories;
import com.base.common.persistent.dao.TicketMapper;
import com.base.common.persistent.model.Ticket;
import com.base.common.utils.EasyUtil;
import com.cjy.common.bean.Files;
import com.cjy.userReport.service.base.TicketBaseServiceImpl;
import com.cjy.userReport.service.complaint.ComplaintService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("complaintService")
public class ComplaintServiceImpl extends TicketBaseServiceImpl implements
		ComplaintService {

	@Resource
	private EasyUtil easyUtil;

	@Resource
	private TicketMapper ticketMapper;

	/**
	 * 生成投诉工单工单号
	 * 
	 * @param tickets
	 * @return
	 */
	public String getComlaintCode(List<Ticket> tickets) {
		try {
			tickets = ticketMapper
					.queryLastCodeByCategories(TicketCategories.COMPLAINT
							.getValue());
		} catch (Exception e) {
			e.printStackTrace();
		}
		String code = "";
		DecimalFormat df = new DecimalFormat(ConfigConstants.TICKET_CODE_NUMS);
		if (tickets.size() != 0) {
			String lastedCode = tickets.get(0).getCode();
			code = df
					.format(Integer.parseInt(lastedCode.substring(1).trim()) + 1);
		} else {
			code = df.format(1);
		}
		code = TicketCategories.COMPLAINT.getName() + code;
		return code;
	}

	@Override
	public List<ComplaintTicket> getComplaintMessagesByUser(int userId, int page) {
		List<ComplaintTicket> complaintTickets = new ArrayList<ComplaintTicket>();
		List<Ticket> tickets = ticketMapper.queryLastByCategoriesAndUserId(
				getStartNum(page), Integer.parseInt(ConfigConstants.INFOS_LIST_NUM),
				TicketCategories.COMPLAINT.getValue(), userId);
		for (Ticket ticket : tickets) {
			ComplaintTicket complaintTicket = new ComplaintTicket(
					ticket.getId(), ticket.getCode(), ticket.getAddress(),
					ticket.getContext(), ticket.getCategories(),
					ticket.getGetTime(), ticket.getFinishTime(),
					ticket.getStatus(), ticket.getPicUrls(),
					ticket.getRemarks(), ticket.getUser().getId(),
					ticket.getTicketScore());
			complaintTickets.add(complaintTicket);
		}
		return complaintTickets;
	}

	@Transactional
	@Override
	public String subComplaintMessage(Ticket ticket, Files files,
			String basePath, int bid) {
		// 设置接单时间
		ticket.setGetTime(easyUtil.getDateStrByGeneral(new Date()));

		// 设置工单为临时工单
		ticket.setCategories(TicketCategories.COMPLAINT.getValue());

		// 设置工单号，查询最新工单号，并在基础上加1
		List<Ticket> tickets = new ArrayList<Ticket>();
		String code = getComlaintCode(tickets);
		ticket.setCode(code);

		// 图片相对保存地址
		String sysImagesFile = "/resources/images/tickets" + "/" + code;

		// 保存图片
		String[] fileNames = files.getFileFileName();
		String picUrls = "";
		try {
			if (bid == 2) {
				picUrls = easyUtil.downloadFile(ticket.getPicUrls(), basePath,
						sysImagesFile, picUrls);
			} else {
				if (fileNames != null && fileNames.length != 0) {
					picUrls = easyUtil.savePictures(files.getFile(), basePath,
							sysImagesFile, fileNames, picUrls);
				}
			}
			ticket.setPicUrls(picUrls);
		} catch (IOException e) {
			return null;
		}
		if (ticketMapper.addTicket(ticket) > 0) {
			return code;
		}
		return null;
	}
}
